package com.anhry.app.safety.manage.oa.struts.action;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.oa.bean.MailBean;
import com.anhry.app.safety.manage.oa.bean.TbOaMail;
import com.anhry.app.safety.manage.oa.bean.TbOaMailFj;
import com.anhry.app.safety.manage.oa.bean.TbOaMailUser;
import com.anhry.app.safety.manage.oa.bean.TbOaMailView;
import com.anhry.app.safety.manage.oa.struts.base.OABaseAction;
import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.bean.TbUserGrade;
import com.anhry.app.safety.manage.system.bean.vo.SystemBean;
import com.anhry.app.safety.util.DateFormater;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.Page;
import com.anhry.app.safety.util.UploadFile;
import com.anhry.app.safety.util.Util;
import com.anhry.app.safety.util.UtilMethod;

/**
 * 
 * @author chen
 * @date 2012-7-31
 */

@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class MailAction extends OABaseAction {

	private static final long serialVersionUID = 1L;

	private static Logger logger = Logger.getLogger(MailAction.class);

	private List<TbUserGrade> gradeList; // 用户级别列表

	private List<TbUser> userList; // 用户列表

	private List<TbOaMailUser> inMailList;// 收件箱列表

	private List<TbOaMail> mailList;// 已发送邮件列表

	private List<TbOaMailFj> fjList;// 邮件附件列表

	private List<TbOaMailView> mailDeleList;

	private MailBean mailView;

	private String addUser;

	private List<TbOaMail> draftMailList;

	private List<TbUser> userNameList;

	private Long fjId;

	private TbOaMailUser mailUser;

	private TbUser user;

	private String arrayName;

	private List<TbOaMailUser> mailUserList;

	private TbOaMail mail;

	private String search;

	private String keyWord;

	private String selects;// 已选择收件人

	private String userGrade;// 用户级别

	private String userDept;// 用户部门

	private String userUnit;// 用户单位

	private String userZws;

	private String isSubmit;// 存储类型0:直接发送1：存储草稿箱

	private Long id;

	private String ids;

	private String forward;// 跳转类型

	private boolean deleOk;

	private boolean dropOk;

	private boolean reSumeOk;

	private String mailType;

	private String addressee;// 收件人

	private String mailTitle;// 邮件标题

	private File[] myFile;// 附件

	private String[] myFileFileName;// 附件标题

	private String[] myFileContentType;// 附件类型

	private String mailContent;// 邮件内容

	private String isExigence;// 紧急程度0:普通 1：紧急

	private String isReturn;// 是否回执0:不需要 1：需要

	private String isNote;// 是否短信提醒 0：不需要1：需要

	private String noteDay;// 短信提醒天数

	private String noteHour;// 短信提醒小时

	private String noteSec;// 短信提醒分钟

	private String noteContent;// 短信提醒内容

	private String beginTime;

	private String endTime;

	private String cGrade;

	private String cUnit;

	private String cDept;

	private String cRole;

	private String cZw;

	private List zwList;

	private List roleList;

	private String str_num;

	private String str_size;

	/**
	 * 解码方法
	 * 
	 * @author zhangpeng
	 */
	private void decodeKey() {
		if (null == this.getSearch() && (keyWord != null && !"".equals(keyWord))) {
			try {
				keyWord = this.contextPvd.decode(keyWord);
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("keyword解码异常：" + e.getMessage());
			}
		}
	}

	/**
	 * 跳转到写信页面
	 * 
	 * @return
	 */
	public String toSend() {
		setResult("mail_send");
		return "success";
	}

	/**
	 * 查询所有收件人列表
	 * 
	 * @return
	 */
	public String userList() {
		try {
			// 是否解码
			decodeKey();
			int rowCount = 0;
			int num = (this.num != null && !"".equals(this.num)) ? Integer.parseInt(this.num) : 1;
			int size = (this.size != null && !"".equals(this.size)) ? Integer.parseInt(this.size) : 5;
			String hql = this.getQuaryHql();
			List objSize = this.systemService.getUserList(hql, 0, 0);
			if (objSize != null && objSize.size() > 0) {
				rowCount = Integer.parseInt(objSize.get(0).toString());
			}
			Page page = new Page(size, num, rowCount);
			this.setPage(page);
			hql += "order by t.gradeId,t.unitId,t.deptId";
			this.setUserList(this.systemService.getUserList(hql, page.getStartRow(), page.getSize()));
			this.setGradeList((List) this.contextPvd.getServletContextAttr(Util.USER_GRADE_LIST));// 用户级别
			this.setRoleList(this.systemService.getRoleList("0", 0, -1));// 角色列表
			this.setZwList(SystemBean.getZwList());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("MailAction中userList()方法：" + e);
		}
		setResult("user_list");
		return "success";
	}

	/**
	 * 邮件发送(发送和修改后发送)
	 * 
	 * @return
	 */
	public String mailSendOrUpdate() {
		TbOaMail mail = null;
		TbUser user = (TbUser) this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		try {
			if (null != id) {// 修改后发送
				mail = (TbOaMail) this.mailService.getUtilDao().getObjectById(TbOaMail.class, this.getId());
				// 设置邮件参数
				setMailProperties(mail);
				this.mailService.getUtilDao().update(mail);
				// 存储发送邮件
				sendMail(mail);
				// 上传附件
				uploadAttachments(myFile, user, mail);
			} else {// 添加邮件发送
				mail = new TbOaMail();
				mail.setIsDele("0");
				setMailProperties(mail);
				mail.setTbUser(user);
				mail.setMailAddressee(this.getAddressee());
				mail.setIsDrop("0");
				this.mailService.getUtilDao().save(mail);
				// 存储发送邮件
				sendMail(mail);
				// 上传附件
				uploadAttachments(myFile, user, mail);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("MailAction中mailSendOrUpdate()方法：" + e);
		}
		if (null != this.getIsSubmit() && !"".equals(this.getIsSubmit()) && "0".equals(this.getIsSubmit())) {// 是否发送
			return mailList();
		} else {
			return draftMailList();
		}
	}

	/**
	 * 存储发送邮件
	 * 
	 * @param mail
	 * @throws Exception
	 */
	private void sendMail(TbOaMail mail) throws Exception {
		if (null != this.getIsSubmit() && !"".equals(this.getIsSubmit()) && "0".equals(this.getIsSubmit())) {// 邮件发送
			if (null != addressee && !"".equals(addressee)) {
				String[] ids = addressee.split(",");
				TbOaMailUser mu;
				for (int i = 0; i < ids.length; i++) {
					mu = new TbOaMailUser();
					mu.setAddresseeId((TbUser) this.utilService.getObjectById(TbUser.class, Long.parseLong(ids[i])));
					mu.setIsDele("0");
					mu.setIsRead("0");// 0：未读1：已读
					mu.setIsDrop("0");
					mu.setIsReturn(mail.getIsReturn());
					mu.setTbOaMail(mail);
					mu.setIsNote("0");
					mu.setMailDate(mail.getMailTime());
					this.mailService.getUtilDao().save(mu);
				}
			}
		}
	}

	/**
	 * 草稿箱
	 * 
	 * @return
	 */
	public String draftMailList() {
		try {
			TbUser user = (TbUser) this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			rowCount = this.utilService.getHqlQuerySize("select count(*) from TbOaMail as draft where draft.isDele=0 and draft.isDrop=0 and draft.isSubmit=1 and draft.tbUser.id='" + user.getId() + "'");
			// 分页设置
			setPagingParameters();
			this.setDraftMailList((List) this.utilService.getHqlQuery("from TbOaMail as draft where draft.isDele=0 and draft.isDrop=0 and draft.isSubmit=1 and draft.tbUser.id='" + user.getId() + "' order by draft.mailTime desc", page.getStartRow(), page.getSize()));
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中draftMailList()方法：+" + e);
		}
		setResult("mail_draftbox_list");
		return "success";
	}

	/**
	 * 邮件删除(移送到已删除文件夹)
	 * 
	 * @return
	 */
	public String mailDele() {
		try {
			if (null != this.getIds() && !"".equals(this.getIds())) {
				// 页面传过来的name为ids的checkbox数据字符串是1,2,3形式的，可以直接用于in子句中
				this.utilService.deleteBySql("update Tb_oa_mail m set m.is_dele='1' where m.id in (" + ids + ")");
				this.setDeleOk(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction method:mailDele()" + e);
		}
		if ("1".equals(this.getForward())) {
			return this.draftMailList();// 跳转到草稿箱
		}
		if ("2".equals(this.getForward())) {
			return this.inMailList();// 收件箱
		}
		if ("3".equals(this.getForward())) {
			return this.deleMailList();// 已删除
		} else {
			return this.mailList();// 跳转到已发送
		}
	}

	/**
	 * 邮件删除（彻底删除）
	 * 
	 * @return
	 */
	public String mailDrop() {
		try {
			if (null != this.getIds() && !"".equals(this.getIds())) {
				this.utilService.deleteBySql("update Tb_oa_mail m set m.is_drop='1' where m.id in (" + ids + ")");
				this.setDropOk(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction method:mailDrop()" + e);
		}
		if ("1".equals(this.getForward())) {
			return this.draftMailList();// 跳转到草稿箱
		}
		if ("2".equals(this.getForward())) {
			return this.inMailList();// 收件箱
		}
		if ("3".equals(this.getForward())) {
			return this.deleMailList();// 已删除
		} else {
			return this.mailList();// 跳转到已发送
		}
	}

	/**
	 * 收件箱列表
	 * 
	 * @return
	 */
	public String inMailList() {
		try {
			decodeKey();// 解码
			TbUser user = (TbUser) this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			rowCount = this.mailService.loadMailList(keyWord, this.getBeginTime(), this.getEndTime(), user, "0", null);
			setPagingParameters();
			this.setInMailList(this.mailService.loadMailList(keyWord, this.getBeginTime(), this.getEndTime(), user, "0", null, page.getStartRow(), page.getSize()));
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中inMailList()方法：+" + e);
		}
		setResult("mail_inbox_list");
		return "success";
	}

	/**
	 * 已发送邮件列表
	 * 
	 * @return
	 */
	public String mailList() {
		try {
			decodeKey();// 解码
			TbUser user = (TbUser) this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			rowCount = this.mailService.loadSendMailList(keyWord, this.getBeginTime(), this.getEndTime(), user, "0");
			setPagingParameters();
			this.setMailList(this.mailService.loadSendMailList(keyWord, this.getBeginTime(), this.getEndTime(), user, "0", page.getStartRow(), page.getSize()));
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中mailList()方法：+" + e);
		}
		setResult("mail_send_list");
		return "success";
	}

	/**
	 * 草稿箱邮件编辑
	 * 
	 * @return
	 */
	public String toDraftEdit() {
		try {
			if (null != this.getId() && !"".equals(this.getId())) {
				mail = (TbOaMail) this.utilService.getObjectById(TbOaMail.class, this.getId());
				if (null != mail) {
					this.setUserNameList((List<TbUser>) this.utilService.getHqlQuery("from TbUser user where user.id in(" + mail.getMailAddressee() + ")"));
					this.setFjList((List<TbOaMailFj>) this.utilService.getHqlQuery("from TbOaMailFj fj left join fetch fj.tbOaMail where fj.tbOaMail.id='" + mail.getId() + "'"));
					setResult("mail_draftbox_edit");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中toDraftEdit()方法：+" + e);
		}
		return ERROR;
	}

	/**
	 * 异步附件删除
	 */
	public void deleFj() {
		this.contextPvd.getResponse().setContentType("charset=UTF-8");
		TbOaMailFj fj = null;
		try {
			if (null != this.getFjId() && !"".equals(this.getFjId())) {
				fj = (TbOaMailFj) this.utilService.getObjectById(TbOaMailFj.class, this.getFjId());
				if (null != fj) {
					UploadFile.removeFile(this.contextPvd.getRequest().getSession().getServletContext().getRealPath("")+ "/" + fj.getFjPath());
					//UploadFile.removeFile(fj.getFjPath());// path 已改为绝对路径
					this.utilService.delete(fj);
				}
				this.contextPvd.getResponse().getWriter().write("1");
			} else {
				this.contextPvd.getResponse().getWriter().write("2");
			}
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction deleFj()方法：" + e);
		}
	}

	/**
	 * 收件箱邮件查看
	 * 
	 * @return
	 */
	public String inMailShow() {
		TbOaMailUser muser = null;
		try {
			this.setKeyWord(this.contextPvd.encode(this.contextPvd.decode(this.getKeyWord())));
			if (null != this.getId() && !"".equals(this.getId())) {
				muser = (TbOaMailUser) this.utilService.getObjectById(TbOaMailUser.class, this.getId());
				if (null != muser) {
					if ("0".equals(muser.getIsRead())) {
						muser.setIsRead("1");// 修改阅读状态（标记为已读）
						muser.setReadTime(new Date());// 阅读时间
						this.utilService.update(muser);
					}
					this.setMailUser(muser);
					mail = (TbOaMail) this.utilService.getObjectById(TbOaMail.class, muser.getTbOaMail().getId());
					if (null != mail) {
						this.setUserNameList((List<TbUser>) this.utilService.getHqlQuery("from TbUser user where user.id in(" + mail.getMailAddressee() + ")"));
						this.setFjList((List<TbOaMailFj>) this.utilService.getHqlQuery("from TbOaMailFj fj left join fetch fj.tbOaMail where fj.tbOaMail.id='" + mail.getId() + "'"));
					}
					setResult("mail_inbox_show");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中inMailShow()方法：+" + e);
		}
		return ERROR;
	}

	/**
	 * 收件箱邮件删除(移除到已删除文件夹)
	 * 
	 * @return
	 */
	public String inMailDele() {
		try {
			if (null != this.getIds() && !"".equals(this.getIds())) {
				this.utilService.deleteBySql("update tb_oa_mail_user u set u.is_dele = '1' where u.id in (" + ids + ")");
				this.setDeleOk(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中inMailDele()方法：+" + e);
		}
		return this.inMailList();
	}

	/**
	 * 收件箱邮件删除（彻底删除）
	 * 
	 * @return
	 */
	public String inMailDrop() {
		try {
			if (null != this.getIds() && !"".equals(this.getIds())) {
				this.utilService.deleteBySql("update tb_oa_mail_user u set u.is_drop = '1' where u.id in (" + ids + ")");
				this.setDropOk(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中inMailDrop()方法：+" + e);
		}
		return this.inMailList();
	}

	/**
	 * 跳转到邮件回复页面
	 * 
	 * @return
	 */
	public String mailBack() {
		TbOaMail mail = null;
		TbUser user = null;
		try {
			if (null != this.getId() && !"".equals(this.getId())) {
				mail = (TbOaMail) this.utilService.getObjectById(TbOaMail.class, this.getId());
				if (null != mail) {
					this.setMail(mail);
				}
			}
			if (null != this.getAddressee() && !"".equals(this.getAddressee())) {
				user = (TbUser) this.utilService.getObjectById(TbUser.class, Long.parseLong(this.getAddressee()));
				if (null != user) {
					this.setUser(user);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中mailBack()方法：+" + e);
		}
		setResult("mail_back");
		return "success";
	}

	/**
	 * 已删除邮件列表
	 * 
	 * @return
	 */
	public String deleMailList() {
		TbUser user = (TbUser) this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		try {
			rowCount = this.utilService.getHqlQuerySize("select count(*) from TbOaMailView mail where mail.isDele=1 and mail.isDrop=0 and mail.mailUser='" + user.getId() + "'");
			setPagingParameters();
			this.setMailDeleList(this.mailService.loadMailDeleList(user, page.getStartRow(), page.getSize()));
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中deleMailList()方法：+" + e);
		}
		setResult("mail_dele_list");
		return "success";
	}

	/**
	 * 已删除邮件查看
	 * 
	 * @return
	 */
	public String deleMailShow() {
		MailBean mail = new MailBean();
		TbUser user = (TbUser) this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		StringBuffer uname = new StringBuffer();
		try {
			if (null != this.getId() && !"".equals(this.getId()) && null != this.getMailType() && !"".equals(this.getMailType())) {
				if ("1".equals(this.getMailType())) {// 已发送的邮件
					TbOaMail om = (TbOaMail) this.utilService.getObjectById(TbOaMail.class, this.getId());
					if (null != om) {
						mail.setId(om.getId());
						mail.setIsExigence(om.getIsExigence());
						// 查询收件人列表（姓名）
						List<TbUser> ulist = (List<TbUser>) this.utilService.getHqlQuery("from TbUser user where user.id in(" + om.getMailAddressee() + ")");
						// 拼接页面显示收件人
						if (null != ulist && ulist.size() > 0) {
							for (int i = 0; i < ulist.size(); i++) {
								uname.append(ulist.get(i).getUserRealName() + "; ");
							}
							uname.setLength(uname.length() - 2);
						}
						mail.setMailAddsUser(uname.toString());
						mail.setMailContent(om.getMailContent());
						mail.setMailTime(om.getMailTime());
						mail.setMailTitle(om.getMailTitle());
						mail.setMailType("1");
						mail.setMailUser(user.getUserRealName());
						this.setFjList((List<TbOaMailFj>) this.utilService.getHqlQuery("from TbOaMailFj fj left join fetch fj.tbOaMail where fj.tbOaMail.id='" + om.getId() + "'"));
					}
				} else {// 邮件箱邮件
					TbOaMailUser mu = (TbOaMailUser) this.utilService.getObjectById(TbOaMailUser.class, this.getId());
					if (null != mu) {
						TbOaMail om = (TbOaMail) this.utilService.getObjectById(TbOaMail.class, mu.getTbOaMail().getId());
						mail.setId(mu.getId());
						mail.setIsExigence(om.getIsExigence());
						mail.setMailAddsUser(mu.getAddresseeId().getUserRealName());
						mail.setMailContent(om.getMailContent());
						mail.setMailTime(om.getMailTime());
						mail.setMailTitle(om.getMailTitle());
						mail.setMailType("2");
						mail.setMailUser(om.getTbUser().getUserRealName());
						mail.setMailWeek(om.getMailWeek());
						this.setFjList((List<TbOaMailFj>) this.utilService.getHqlQuery("from TbOaMailFj fj left join fetch fj.tbOaMail where fj.tbOaMail.id='" + om.getId() + "'"));
					}
				}
				this.setMailView(mail);
			} else {
				return ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中deleMailShow()方法：+" + e);
		}
		setResult("mail_dele_show");
		return "success";
	}

	/**
	 * 已发送邮件查看
	 * 
	 * @return
	 */
	public String sendMailShow() {
		StringBuffer uname = new StringBuffer();
		try {
			this.setKeyWord(this.contextPvd.encode(this.contextPvd.decode(this.getKeyWord())));
			if (null != this.getId() && !"".equals(this.getId())) {
				mail = (TbOaMail) this.utilService.getObjectById(TbOaMail.class, this.getId());
				if (null != mail) {
					// 查询收件人列表（姓名）
					List<TbUser> ulist = (List<TbUser>) this.utilService.getHqlQuery("from TbUser user where user.id in(" + mail.getMailAddressee() + ")");
					this.setFjList((List<TbOaMailFj>) this.utilService.getHqlQuery("from TbOaMailFj fj left join fetch fj.tbOaMail where fj.tbOaMail.id='" + mail.getId() + "'"));
					if (null != ulist && ulist.size() > 0) {
						for (int i = 0; i < ulist.size(); i++) {
							uname.append(ulist.get(i).getUserRealName() + "; ");
						}
						uname.setLength(uname.length() - 2);
					}
					this.setArrayName(uname.toString());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中sendMailShow()方法：+" + e);
		}
		setResult("mail_send_show");
		return "success";
	}

	/**
	 * 已删除列表中邮件彻底删除
	 * 
	 * @return
	 */
	public String deleMailDrop() {
		TbOaMail mail = null;
		TbOaMailUser mu = null;
		try {
			if (null != this.getIds() && !"".equals(this.getIds())) {
				String[] ids = this.getIds().split(",");
				for (String id : ids) {
					String[] idt = id.split(":");
					if (idt[1].equals("1")) {// 已发送邮件
						mail = (TbOaMail) this.utilService.getObjectById(TbOaMail.class, Long.parseLong(idt[0]));
						if (null != mail) {
							mail.setIsDrop("1");
							this.utilService.update(mail);
						}
					} else {// 收件箱邮件
						mu = (TbOaMailUser) this.utilService.getObjectById(TbOaMailUser.class, Long.parseLong(idt[0]));
						if (null != mu) {
							mu.setIsDrop("1");
							this.utilService.update(mu);
						}
					}
				}
				this.setDropOk(true);

			} else {
				return ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中deleMailDrop()方法：+" + e);
		}
		return this.deleMailList();
	}

	/**
	 * 已删除邮件还原
	 * 
	 * @return
	 */
	public String deleMailResume() {
		TbOaMail mail = null;
		TbOaMailUser mu = null;
		try {
			if (null != this.getIds() && !"".equals(this.getIds())) {
				String[] ids = this.getIds().split(",");
				for (String id : ids) {
					String[] idt = id.split(":");
					if (idt[1].equals("1")) {// 已发送邮件
						mail = (TbOaMail) this.utilService.getObjectById(TbOaMail.class, Long.parseLong(idt[0]));
						if (null != mail) {
							mail.setIsDele("0");
							this.utilService.update(mail);
						}
					} else {// 收件箱邮件
						mu = (TbOaMailUser) this.utilService.getObjectById(TbOaMailUser.class, Long.parseLong(idt[0]));
						if (null != mu) {
							mu.setIsDele("0");
							this.utilService.update(mu);
						}
					}
				}
				this.setReSumeOk(true);
			} else {
				return ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中deleMailResume()方法：+" + e);
		}
		return this.deleMailList();
	}

	/**
	 * 查看收件人是否已读
	 * 
	 * @return
	 */
	public String mailUserRead() {
		try {
			this.setKeyWord(this.contextPvd.encode(this.contextPvd.decode(this.getKeyWord())));
			if (null != this.getId() && !"".equals(this.getId())) {
				TbOaMail mail = (TbOaMail) this.utilService.getObjectById(TbOaMail.class, this.getId());
				this.setMail(mail);
				rowCount = this.utilService.getHqlQuerySize("select count(*) from TbOaMailUser mu where mu.tbOaMail.id='" + this.getId() + "'");
				setPagingParameters();
				this.setMailUserList((List<TbOaMailUser>) this.utilService.getHqlQuery("from TbOaMailUser mu left join fetch mu.tbOaMail left join fetch mu.addresseeId left join fetch mu.addresseeId.tbUserUnit left join fetch mu.addresseeId.tbUserDept  where mu.tbOaMail.id='" + this.getId() + "'", page.getStartRow(), page.getSize()));
			} else {
				return ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中mailUserRead()方法：+" + e);
		}
		setResult("mail_send_read");
		return "success";
	}

	/**
	 * 设置邮件相关属性值
	 * 
	 * @param mail
	 * @return
	 */
	private String setMailProperties(TbOaMail mail) {
		try {
			if (null != this.getIsExigence() && !"".equals(this.getIsExigence()) && "1".equals(this.getIsExigence())) {// 紧急程度
				mail.setIsExigence("1");// 紧急
			} else {
				mail.setIsExigence("0");// 普通
			}
			if (null != this.getIsNote() && !"".equals(this.getIsNote()) && "1".equals(this.getIsNote())) {// 短信提醒
				mail.setIsNote("1");// 需要短信提醒
				mail.setNoteContent(this.getNoteContent());
				mail.setNoteDay(this.getNoteDay());
				mail.setNoteHour(this.getNoteHour());
				mail.setNoteSec(this.getNoteSec());
			} else {// 不需要
				mail.setIsNote("0");
				mail.setNoteContent(null);
				mail.setNoteDay("0");
				mail.setNoteHour("0");
				mail.setNoteSec("0");
			}
			if (null != this.getIsReturn() && !"".equals(this.getIsNote()) && "1".equals(this.getIsReturn())) {// 是否回执
				mail.setIsReturn("1");// 是
			} else {
				mail.setIsReturn("0");// 否
			}
			mail.setTbUser(user);
			mail.setMailAddressee(addressee);
			mail.setMailContent(this.getMailContent());
			mail.setMailTime(new Date());
			Calendar cd = Calendar.getInstance();
			int mydate = cd.get(Calendar.DAY_OF_WEEK);
			String nowWeek = "星期六";
			switch (mydate) {
			case 1:
				nowWeek = "星期日";
				break;
			case 2:
				nowWeek = "星期一";
				break;
			case 3:
				nowWeek = "星期二";
				break;
			case 4:
				nowWeek = "星期三";
				break;
			case 5:
				nowWeek = "星期四";
				break;
			case 6:
				nowWeek = "星期五";
				break;
			default:
				nowWeek = "星期六";
				break;
			}
			mail.setMailWeek(nowWeek);
			int year = cd.get(Calendar.YEAR);
			mail.setMailYear(String.valueOf(year));// 年度
			int quarter = DateFormater.getQuarter(cd);
			mail.setMailQuarter(String.valueOf(quarter));// 季度
			int month = cd.get(Calendar.MONTH) + 1;
			mail.setMailMonth(String.valueOf(month));
			mail.setMailTitle(this.getMailTitle());
			if (null != this.getIsSubmit() && !"".equals(this.getIsSubmit()) && "0".equals(this.getIsSubmit())) {// 是否发送
				mail.setIsSubmit("0");// 发送
			} else {
				mail.setIsSubmit("1");// 存草稿
			}
			return "success";
		} catch (RuntimeException e) {
			e.printStackTrace();
			Loggers.error("mailAction:setMailProperties方法出错：" + e);
		}
		return "error";
	}

	/**
	 * 上传附件
	 * 
	 * @param files
	 * @param user
	 * @param mail
	 * @throws Exception
	 */
	private void uploadAttachments(File[] files, TbUser user, TbOaMail mail) throws Exception {
		TbOaMailFj fj;
		File f;
		if (null != files && files.length > 0) {
			for (int j = 0; j < files.length; j++) {
				fj = new TbOaMailFj();
				fj.setFjTitle(myFileFileName[j]);
				fj.setFjSize(UtilMethod.getFjSize(String.valueOf(files[j].length())));
				String path = Util.SAFETY_FILE + File.separator + Util.FILE_FOLDER + File.separator + Util.OA_Mail + File.separator;
				f = new File(path);
				if (!f.exists())
					f.mkdirs();
				String file_Name = UploadFile.copy(files[j], path, myFileFileName[j], user.getId().toString());
				String path1 = Util.FILE_FOLDER + File.separator + Util.OA_Mail + File.separator + user.getId() + File.separator + file_Name;
				fj.setFjPath(path1);
				fj.setIsDele("0");
				fj.setTbOaMail(mail);
				this.utilService.save(fj);
			}
		}
	}

	/**
	 * 得到查询hql语句
	 * 
	 * @return
	 */
	public String getQuaryHql() {
		StringBuilder hql = new StringBuilder(" where 1=1 ");
		if (null != this.keyWord && this.keyWord.length() > 0) {
			hql.append(" and t.userName like '%" + keyWord + "%'");
		}
		if (null != this.cGrade && this.cGrade.length() > 0) {
			hql.append(" and t.gradeId=" + this.cGrade);
		}
		if (null != this.cUnit && this.cUnit.length() > 0) {
			hql.append(" and t.unitId=" + this.cUnit);
		}
		if (null != this.cDept && this.cDept.length() > 0) {
			hql.append(" and t.deptId=" + this.cDept);
		}
		if (null != this.cRole && this.cRole.length() > 0) {
			hql.append(" and t.roleId=" + this.cRole);
		}
		if (null != this.cZw && this.cZw.length() > 0) {
			hql.append(" and t.userZw=" + this.cZw);
		}
		return hql.toString();
	}

	// 获取 未读 邮件个数
	public void getNotRead() {
		TbUser user = (TbUser) this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		try {
			rowCount = this.mailService.loadMailList("", "", "", user, "0", "0");
			this.contextPvd.getResponse().getWriter().print(rowCount);
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("MailAction中getNotRead()方法：+" + e);
		}

	}

	public String getSelects() {
		return selects;
	}

	public void setSelects(String selects) {
		this.selects = selects;
	}

	public String getUserDept() {
		return userDept;
	}

	public void setUserDept(String userDept) {
		this.userDept = userDept;
	}

	public String getUserUnit() {
		return userUnit;
	}

	public void setUserUnit(String userUnit) {
		this.userUnit = userUnit;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<TbUser> getUserList() {
		return userList;
	}

	public void setUserList(List<TbUser> userList) {
		this.userList = userList;
	}

	public List<TbUserGrade> getGradeList() {
		return gradeList;
	}

	public void setGradeList(List<TbUserGrade> gradeList) {
		this.gradeList = gradeList;
	}

	public String getAddUser() {
		return addUser;
	}

	public void setAddUser(String addUser) {
		this.addUser = addUser;
	}

	public String getMailTitle() {
		return mailTitle;
	}

	public void setMailTitle(String mailTitle) {
		this.mailTitle = mailTitle;
	}

	public File[] getMyFile() {
		return myFile;
	}

	public void setMyFile(File[] myFile) {
		this.myFile = myFile;
	}

	public String getMailContent() {
		return mailContent;
	}

	public void setMailContent(String mailContent) {
		this.mailContent = mailContent;
	}

	public String getIsExigence() {
		return isExigence;
	}

	public void setIsExigence(String isExigence) {
		this.isExigence = isExigence;
	}

	public String getIsReturn() {
		return isReturn;
	}

	public void setIsReturn(String isReturn) {
		this.isReturn = isReturn;
	}

	public String getIsNote() {
		return isNote;
	}

	public void setIsNote(String isNote) {
		this.isNote = isNote;
	}

	public String getNoteDay() {
		return noteDay;
	}

	public void setNoteDay(String noteDay) {
		this.noteDay = noteDay;
	}

	public String getNoteHour() {
		return noteHour;
	}

	public void setNoteHour(String noteHour) {
		this.noteHour = noteHour;
	}

	public String getNoteSec() {
		return noteSec;
	}

	public void setNoteSec(String noteSec) {
		this.noteSec = noteSec;
	}

	public String getNoteContent() {
		return noteContent;
	}

	public void setNoteContent(String noteContent) {
		this.noteContent = noteContent;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getIsSubmit() {
		return isSubmit;
	}

	public void setIsSubmit(String isSubmit) {
		this.isSubmit = isSubmit;
	}

	public List<TbOaMail> getDraftMailList() {
		return draftMailList;
	}

	public void setDraftMailList(List<TbOaMail> draftMailList) {
		this.draftMailList = draftMailList;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getForward() {
		return forward;
	}

	public void setForward(String forward) {
		this.forward = forward;
	}

	public boolean isDeleOk() {
		return deleOk;
	}

	public void setDeleOk(boolean deleOk) {
		this.deleOk = deleOk;
	}

	public List<TbOaMailUser> getInMailList() {
		return inMailList;
	}

	public void setInMailList(List<TbOaMailUser> inMailList) {
		this.inMailList = inMailList;
	}

	public List<TbOaMail> getMailList() {
		return mailList;
	}

	public void setMailList(List<TbOaMail> mailList) {
		this.mailList = mailList;
	}

	public List<TbUser> getUserNameList() {
		return userNameList;
	}

	public void setUserNameList(List<TbUser> userNameList) {
		this.userNameList = userNameList;
	}

	public TbOaMail getMail() {
		return mail;
	}

	public void setMail(TbOaMail mail) {
		this.mail = mail;
	}

	public List<TbOaMailFj> getFjList() {
		return fjList;
	}

	public void setFjList(List<TbOaMailFj> fjList) {
		this.fjList = fjList;
	}

	public Long getFjId() {
		return fjId;
	}

	public void setFjId(Long fjId) {
		this.fjId = fjId;
	}

	public TbOaMailUser getMailUser() {
		return mailUser;
	}

	public void setMailUser(TbOaMailUser mailUser) {
		this.mailUser = mailUser;
	}

	public TbUser getUser() {
		return user;
	}

	public void setUser(TbUser user) {
		this.user = user;
	}

	public boolean isDropOk() {
		return dropOk;
	}

	public void setDropOk(boolean dropOk) {
		this.dropOk = dropOk;
	}

	public List<TbOaMailView> getMailDeleList() {
		return mailDeleList;
	}

	public void setMailDeleList(List<TbOaMailView> mailDeleList) {
		this.mailDeleList = mailDeleList;
	}

	public MailBean getMailView() {
		return mailView;
	}

	public void setMailView(MailBean mailView) {
		this.mailView = mailView;
	}

	public String getMailType() {
		return mailType;
	}

	public void setMailType(String mailType) {
		this.mailType = mailType;
	}

	public String getArrayName() {
		return arrayName;
	}

	public void setArrayName(String arrayName) {
		this.arrayName = arrayName;
	}

	public boolean isReSumeOk() {
		return reSumeOk;
	}

	public void setReSumeOk(boolean reSumeOk) {
		this.reSumeOk = reSumeOk;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}

	public List<TbOaMailUser> getMailUserList() {
		return mailUserList;
	}

	public void setMailUserList(List<TbOaMailUser> mailUserList) {
		this.mailUserList = mailUserList;
	}

	public String getUserZws() {
		return userZws;
	}

	public void setUserZws(String userZws) {
		this.userZws = userZws;
	}

	public String getAddressee() {
		return addressee;
	}

	public void setAddressee(String addressee) {
		this.addressee = addressee;
	}

	public String[] getMyFileContentType() {
		return myFileContentType;
	}

	public void setMyFileContentType(String[] myFileContentType) {
		this.myFileContentType = myFileContentType;
	}

	public String[] getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String[] myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	public String getUserGrade() {
		return userGrade;
	}

	public void setUserGrade(String userGrade) {
		this.userGrade = userGrade;
	}

	public String getCDept() {
		return cDept;
	}

	public void setCDept(String dept) {
		cDept = dept;
	}

	public String getCGrade() {
		return cGrade;
	}

	public void setCGrade(String grade) {
		cGrade = grade;
	}

	public String getCRole() {
		return cRole;
	}

	public void setCRole(String role) {
		cRole = role;
	}

	public String getCUnit() {
		return cUnit;
	}

	public void setCUnit(String unit) {
		cUnit = unit;
	}

	public String getCZw() {
		return cZw;
	}

	public void setCZw(String zw) {
		cZw = zw;
	}

	public List getZwList() {
		return zwList;
	}

	public void setZwList(List zwList) {
		this.zwList = zwList;
	}

	public List getRoleList() {
		return roleList;
	}

	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}

	public String getStr_num() {
		return str_num;
	}

	public void setStr_num(String str_num) {
		this.str_num = str_num;
	}

	public String getStr_size() {
		return str_size;
	}

	public void setStr_size(String str_size) {
		this.str_size = str_size;
	}

}
